package com.weiaibenpao.demo.chislim.ui.activity.discover;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.MarathonBean;
import com.weiaibenpao.demo.chislim.presenter.MarathonPresenter;
import com.weiaibenpao.demo.chislim.ui.adapter.MarathonListAdapter;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.view.MarathonView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MatchActivity extends BaseActivity<MarathonView, MarathonPresenter> implements MarathonView,
        BaseQuickAdapter.RequestLoadMoreListener,OnRefreshListener ,BaseQuickAdapter.OnItemClickListener{


    @BindView(R.id.myMatch)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private int page =1;
    private MarathonListAdapter mAdapter;
    private View view;

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_match;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        iniAdapter();
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setRefreshing(true);

        doRefresh();

    }

    private void doRefresh() {
        page=1;
        mAdapter.setEnableLoadMore(false);
        doRequest();
    }

    private void doRequest() {
        mPresenter.getMarathonList(page, Constant.DEFAULT_PAGE_SIZE);

    }

    private void iniAdapter() {
        mAdapter = new MarathonListAdapter(R.layout.marathon_list_item);
        mAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
         view = LayoutInflater.from(this).inflate(R.layout.layout_empty, null);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setRefreshing(true);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public MarathonPresenter initPresenter() {
        return new MarathonPresenter();
    }

    @Override
    public void getMarathonListSuccess(MarathonBean marathonBean) {
        swipeLayout.setRefreshing(false);
        mAdapter.setEmptyView(view);

        List<MarathonBean.MarathoListBean> marathoList = marathonBean.getMarathoList();
        boolean isRefresh=page==1;
        setData(marathoList,isRefresh);
    }

    private void setData( List<MarathonBean.MarathoListBean> marathoList,boolean isRefresh) {
        int size=marathoList!=null?marathoList.size():0;
        if (page==1){
            mAdapter.setNewData(marathoList);

        }else {
            mAdapter.addData(marathoList);
        }
        if (size < Constant.DEFAULT_PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd();
        } else {
            mAdapter.loadMoreComplete();
        }
    }


    @OnClick(R.id.left_image)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_image:
                finish();
                break;

        }
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        doRequest();
    }

    @Override
    public void onRefresh() {
        swipeLayout.setRefreshing(true);
        doRefresh();
    }



//    @Override
//    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
////        malasong/detail.html?marathonId=4&sportType=7&userId=
//
//    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        MarathonWebActivity.startSelf(this,mAdapter.getItem(position).getDetailUrl());
        finish();

    }
}
